package cz.proks.easyhelpdesk.gwt.activity.base;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.RequiresResize;

import cz.proks.easyhelpdesk.api.model.User;
import cz.proks.easyhelpdesk.gwt.ClientFactory;
import cz.proks.easyhelpdesk.gwt.Globals;
import cz.proks.easyhelpdesk.gwt.place.LogoutPlace;
import cz.proks.easyhelpdesk.gwt.place.UserPlace;
import cz.proks.easyhelpdesk.gwt.place.WelcomePlace;
import cz.proks.easyhelpdesk.gwt.place.base.HelpdeskPlace;
import cz.proks.easyhelpdesk.gwt.ui.base.RestrictedView;
import cz.proks.easyhelpdesk.gwt.ui.widget.NoPermissionLabel;
import cz.proks.easyhelpdesk.gwt.utils.ApplicationDataLoader;

public abstract class RestrictedActivity<ThePlace extends HelpdeskPlace> extends BaseActivity<ThePlace> implements ApplicationDataLoader.LoadCallback {
    private static final ApplicationDataLoader APP_DATA_LOADER = new ApplicationDataLoader();

    private final NoPermissionLabel noPermissionWidget = new NoPermissionLabel();
    private InitDelegator initDelegator;

    public RestrictedActivity(ThePlace place, ClientFactory clientFactory, PlaceHistoryMapper placeMapper) {
        super(place, clientFactory, placeMapper);
    }

    protected abstract RestrictedView getView();

    @Override
    public final void start(AcceptsOneWidget panel, EventBus eventBus) {
        APP_DATA_LOADER.doLoadOnce(this, panel, eventBus);        
    }
    
    @Override
    public void onLoadComplete(AcceptsOneWidget panel, EventBus eventBus) {
        initActivity();
        routeActivity(panel, eventBus);       
        
       if (getView() != null)
           getView().onResize();
    }
    
    @Override
    public void onLoadFailed(AcceptsOneWidget panel, EventBus eventBus) {
        getClientFactory().getPlaceController().goTo(new WelcomePlace());        
    }

    private void routeActivity(final AcceptsOneWidget panel, final EventBus eventBus) {
        if (hasAccess())
            onPermission(panel, eventBus);

        else
            onNoPermission(panel, eventBus);
    }

    private void initActivity() {
        if (getView() != null) {
            getView().setUserFullName(getFullUserName(Globals.getCurrentUser())); 
            getView().setUserTargetToken(getPlaceMapper().getToken(new UserPlace(Globals.getCurrentUser().getId())));
            getView().setLogoutTargetToken(getPlaceMapper().getToken(new LogoutPlace()));
        }

        if (initDelegator != null)
            initDelegator.onInit();
    }

    protected abstract void onPermission(AcceptsOneWidget panel, EventBus eventBus);

    protected void onNoPermission(AcceptsOneWidget panel, EventBus eventBus) {
        panel.setWidget(noPermissionWidget);
    }

    void setInitDelegator(InitDelegator initDelegator) {
        this.initDelegator = initDelegator;
    }

    protected boolean hasAccess() {
        return Globals.getCurrentUser() != null;
    }

    private String getFullUserName(User user) {
        return user != null ? (user.getName() + " " + user.getSurname()) : "";
    }

    protected interface InitDelegator {
        void onInit();
    }
}
